package com.stock;

import com.kevin.core.util.StringUtils;
import com.kevin.dao.StockDao;
import com.kevin.entity.Stock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class StockService{
	final static String api="http://xueqiu.com/stock/quote_order.json?page=1&size=3000&order=desc&exchange=CN&stockType={}&order=asc&orderby=symbol&column=symbol%2Cname%2Chasexist&orderBy=percent&_=1448808184670";
	
	@Autowired
	StockDao dao;
	
	public List<Stock> queryList(String type){
		String url=StringUtils.fromat(api, type);
		String json=Client.doGet(url);
		Parser parser=new StockParser();
		return (List<Stock>) parser.parse(json);
	}
	
	public void save(String type){
		List<Stock> list = this.queryList(type);
		if(list==null||list.isEmpty()){
			Logger.error("stock list is null or empty,save error.");
			return;
		}
		int size = list.size();
		Logger.info("stock list size:{}", size);
		int i=0;
		for(Stock o:list){
			i+=1;
			o.setType(type);
			dao.save(o);
			Logger.info("["+ size +"] "+i+" completed.");
		}
	}
	
	public void deleteAll(){
		dao.deleteAll();
	}

}
